package com.chaoyi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.PortraitVideo;
import com.chaoyi.service.PortraitVideoService;
import com.chaoyi.util.upload.FileType;
import com.chaoyi.util.upload.UploadRes;
/**
 * 写真视频
 */
@Controller
@MultipartConfig
@RequestMapping("/portrait/video")
public class PortraitVideoController {
	@Resource
	PortraitVideoService portraitVideoService;
	/**
	 * 初始化跳转页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mod =new ModelAndView("/portrait/video/videoList");
		return mod;
	}
	/**
	 * 首页展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("videoList")
	@ResponseBody
	public JSONObject videoList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		int count =portraitVideoService.findCount();
		List<PortraitVideo> list = new ArrayList<PortraitVideo>();
		if(count>0){
			list = portraitVideoService.findList(offset, limit);
		}
		json.put("rows", list);
		json.put("total", count);
		return json;
	}
	
	/**
	 *  新增首页内容跳转页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("addVideo")
	public ModelAndView addVideo(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mod = new ModelAndView("/portrait/video/addVideo");
		return mod;
	}
	/**
	 * 保存新增首页内容
	 */
	@ResponseBody
	@RequestMapping("addVideoSave")
	public JSONObject addVideoSave(HttpServletRequest request, HttpServletResponse response, PortraitVideo entity) {
		JSONObject json = new JSONObject();
		int number = portraitVideoService.addVideoSave(entity);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 修改首页内容
	 */
	@RequestMapping("/alertVideo")
	public ModelAndView alertVideo(HttpServletRequest request, HttpServletResponse response,int id) {
		ModelAndView mod = new ModelAndView("/portrait/video/alertVideo");
		PortraitVideo entity = portraitVideoService.findVideoOne(id);
		mod.addObject("obj",entity);
		return mod;
	}
	
	/**
	 * 保存修改的私播信息
	 */
	@ResponseBody
	@RequestMapping("alertVideoSave")
	public JSONObject alertVideoSave(HttpServletRequest request, HttpServletResponse response, PortraitVideo entity) {
		JSONObject json = new JSONObject();
		int number = portraitVideoService.alertVideoSave(entity);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 删除首页内容
	 */
	@ResponseBody
	@RequestMapping("deleteVideo")
	public JSONObject deleteVideo(HttpServletRequest request, HttpServletResponse response, int id) {
		JSONObject json = new JSONObject();
		int number = portraitVideoService.deleteVideo(id);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	/**
	 * 新增首页封面上传
	 * @param response
	 * @param uploadFile
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadClassifyImg")
	@ResponseBody
	public JSONObject uploadClassifyImg(HttpServletResponse response,@RequestParam("file_data")Part uploadFile) throws IOException{
		String imgUrl = null;
		try{
			imgUrl = portraitVideoService.uploadClassifyImg2WSCDN(uploadFile);
		}catch(Exception e){
			return UploadRes.error("文件上传失败,error:"+e.getMessage());
		}
		JSONObject result = UploadRes.success()
				.setFileUrl(imgUrl)
				.setFileType(FileType.IMAGE)
				.setDelelteUrl("deletefile")
				.putExtraParam("path", imgUrl).toJSON();
		return result;
	}
	/**
	 * 新增首页视频上传
	 * @param response
	 * @param uploadFile
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadClassifyVideo")
	@ResponseBody
	public JSONObject uploadClassifyVideo(HttpServletResponse response,@RequestParam("file_data")Part uploadFile) throws IOException{
		String fileUrl = null;
		try{
			fileUrl = portraitVideoService.uploadClassifyVideo2WSCDN(uploadFile);
		}catch(Exception e){
			return UploadRes.error("文件上传失败,error:"+e.getMessage());
		}
		JSONObject result = UploadRes.success()
				.setFileUrl(fileUrl)
				.setFileType(FileType.IMAGE)
				.setDelelteUrl("deletefile")
				.putExtraParam("path", fileUrl).toJSON();
		return result;
	}
}
